package com.asion.vmovie.activities;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.design.widget.BottomSheetDialog;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.asion.vmovie.R;
import com.asion.vmovie.adapters.XiLieViewAdapter;
import com.asion.vmovie.beans.SeriesDetialsBean;
import com.asion.vmovie.beans.VideoBean;
import com.asion.vmovie.ui.MVPContract.XiLieDetialContract;
import com.asion.vmovie.ui.MVPModel.SeriesModel;
import com.asion.vmovie.ui.MVPPresenter.SeriesPresent;
import com.asion.vmovie.utils.ToastUtil;
import com.superplayer.library.SuperPlayer;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class XiLieActivity extends BaseActivity<SeriesPresent, SeriesModel> implements XiLieDetialContract.View, XiLieViewAdapter.OnItemClickListener, SuperPlayer.OnNetChangeListener {

    @BindView(R.id.view_super_player)
    SuperPlayer viewSuperPlayer;
    @BindView(R.id.video_back)
    ImageView videoBack;
    @BindView(R.id.video_share)
    ImageView videoShare;
    @BindView(R.id.xilie_recycler)
    RecyclerView xilieRecycler;
    private XiLieViewAdapter xiLieViewAdapter;
    private View view;
    private BottomSheetDialog bottomSheetDialog;
    private TextView title;

    public static void showAction(Activity activity, Bundle bundle) {
        Intent intent = new Intent(activity, XiLieActivity.class);
        intent.putExtra("bundle", bundle);
        activity.startActivity(intent);
        activity.overridePendingTransition(R.anim.fade_in,
                R.anim.fade_out);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_xilie;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
    }

    @Override
    public void initView() {
        startProgressDialog();
        bottomSheetDialog = new BottomSheetDialog(this);
        Bundle bundle = getIntent().getBundleExtra("bundle");
        String seriesid = bundle.getString("seriesid");

        videoBack.setOnClickListener(v -> finish());
        xiLieViewAdapter = new XiLieViewAdapter(this);
        xilieRecycler.setItemAnimator(new DefaultItemAnimator());
        xilieRecycler.setLayoutManager(new LinearLayoutManager(this));
        xilieRecycler.setAdapter(xiLieViewAdapter);

        xiLieViewAdapter.setOnItemClickListener(this);

        view = LayoutInflater.from(this).inflate(R.layout.xilie_header, null);
        xiLieViewAdapter.setmHeaderView(view);

        mPresenter.getSeriesDetial(Integer.parseInt(seriesid));

        videoShare.setOnClickListener(c -> {
            View inflate = getLayoutInflater().inflate(R.layout.share_dialog, null);
            inflate.findViewById(R.id.dialog_close).setOnClickListener(v -> bottomSheetDialog.dismiss());
            inflate.findViewById(R.id.dialog_qq).setOnClickListener(v -> ToastUtil.showShort("qq"));
            inflate.findViewById(R.id.dialog_qzone).setOnClickListener(v -> ToastUtil.showShort("qzone"));
            inflate.findViewById(R.id.dialog_vblog).setOnClickListener(v -> ToastUtil.showShort("vblog"));
            inflate.findViewById(R.id.dialog_wechat).setOnClickListener(v -> ToastUtil.showShort("wechat"));
            bottomSheetDialog.setContentView(inflate);
            bottomSheetDialog.show();
        });
    }

    private List<SeriesDetialsBean.DataBean.PostsBean.ListBean> list = new ArrayList<>();

    @Override
    public void showDataSuccess(Object seriesDetialsBean) {
        if(seriesDetialsBean instanceof  SeriesDetialsBean){
            SeriesDetialsBean seriesDetialsBean1 = (SeriesDetialsBean) seriesDetialsBean;
            SeriesDetialsBean.DataBean.PostsBean.ListBean listBean = seriesDetialsBean1.getData().getPosts().get(0).getList().get(0);
            title = (TextView) view.findViewById(R.id.post_title);
            title.setText("第"+listBean.getNumber()+"集 "+listBean.getTitle());
            ((TextView) view.findViewById(R.id.title)).setText(seriesDetialsBean1.getData().getTitle());
            ((TextView) view.findViewById(R.id.weekly)).setText("更新："+seriesDetialsBean1.getData().getWeekly());
            ((TextView) view.findViewById(R.id.update)).setText("集数：更新至"+seriesDetialsBean1.getData().getUpdate_to()+"集");
            ((TextView) view.findViewById(R.id.tag)).setText("类型："+seriesDetialsBean1.getData().getTag_name());
            ((TextView) view.findViewById(R.id.content)).setText(seriesDetialsBean1.getData().getContent());
            for (SeriesDetialsBean.DataBean.PostsBean postsBean : seriesDetialsBean1.getData().getPosts()) {
                for (SeriesDetialsBean.DataBean.PostsBean.ListBean bean : postsBean.getList()) {
                    list.add(bean);
                }
            }
            xiLieViewAdapter.addAll(list);
        }else if(seriesDetialsBean instanceof VideoBean){
            String url = ((VideoBean) seriesDetialsBean).getData().getQiniu_url();
            initPlayer(url);
        }

        stopProgressDialog();

    }

    private void initPlayer(String url) {
        viewSuperPlayer.setNetChangeListener(true)//设置监听手机网络的变化
                .setOnNetChangeListener(this)//实现网络变化的回调
                .play(url);//开始播放视频
        viewSuperPlayer.setScaleType(SuperPlayer.SCALETYPE_FITXY);
        viewSuperPlayer.setPlayerWH(0, viewSuperPlayer.getMeasuredHeight());//设置竖屏的时候屏幕的高度，如果不设置会切换后按照16:9的高度重置
    }

    @Override
    public void onWifi() {
        ToastUtil.showShort("当前网络环境是WIFI");
    }

    @Override
    public void onMobile() {
        ToastUtil.showShort("当前网络环境是手机网络");
    }

    @Override
    public void onDisConnect() {
        ToastUtil.showShort("网络链接断开");
    }

    @Override
    public void onNoAvailable() {
        ToastUtil.showShort("无网络链接");
    }

    /**
     * 下面的这几个Activity的生命状态很重要
     */
    @Override
    protected void onPause() {
        super.onPause();
        if (viewSuperPlayer != null) {
            viewSuperPlayer.onPause();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (viewSuperPlayer != null) {
            viewSuperPlayer.onResume();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (viewSuperPlayer != null) {
            viewSuperPlayer.onDestroy();
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (viewSuperPlayer != null) {
            viewSuperPlayer.onConfigurationChanged(newConfig);
        }
    }

    @Override
    public void onBackPressed() {
        if (viewSuperPlayer != null && viewSuperPlayer.onBackPressed()) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    public void showDataError(Throwable throwable) {

    }

    private View lastView;
    @Override
    public void onItemClick(View view,SeriesDetialsBean.DataBean.PostsBean.ListBean dataBean) {
        if(lastView == null){
            lastView = view;
        }else{
            lastView.findViewById(R.id.play).setVisibility(View.GONE);
            lastView = view;
        }
        view.findViewById(R.id.play).setVisibility(View.VISIBLE);
        title.setText("第"+dataBean.getNumber()+"集 "+dataBean.getTitle());
        mPresenter.getVideoDetial(Integer.parseInt(dataBean.getSeries_postid()));
    }

}
